<template>
    <div>
        <el-dialog

                title="修改课程信息"
                :visible.sync="updateCourseShow"
                :show-close="false"
                :before-close="handleClose">
            <el-form label-position="right" :rules="rules" :model="course" ref="ruleForm">
                <el-form-item label="课程图片">
                    <el-upload
                            :multiple="false"
                            ref="upload"
                            class="upload-demo"
                            action="#"
                            :auto-upload="false"
                            :file-list="HFfileList"
                            :before-upload="beforeUpload"
                            list-type="picture-card"
                            :on-remove="HFhandleRemove"
                            :on-preview="handlePictureCardPreview"
                            :on-change="HFhandleChangeImg">
                        <i class="el-icon-plus"></i>
                        <div slot="tip" class="el-upload__tip">
                            只能上传jpg/png文件，且不超过5Mb
                        </div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="课程名称" prop="name">
                    <el-input v-model="course.name"></el-input>
                </el-form-item>
                <el-form-item label="课程价格" prop="price">
                    <el-input-number v-model="course.price"></el-input-number>
                </el-form-item>
                <el-form-item label="课时" prop="courseTimes">
                    <el-input-number v-model="course.courseTimes"></el-input-number>
                </el-form-item>
            </el-form>

            <el-button type="primary" style="transform: translateY(-10%);float: right" icon="el-icon-upload2"
                       size="mini" @click="confirmUpdate">确认修改
            </el-button>

        </el-dialog>
    </div>
</template>

<script>
    import {updateCourseInfo} from '@/utils/apis/course'
    import upload from '@/utils/methods/upload'

    export default {
        name: "UpdateCourse",
        props: ['updateCourseShow', 'course'],
        data() {
            return {
                imgUrl: '',
                HFfileList: [],
                rules: {
                    name: [{required: true, message: '请输入课程名称', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}],
                }
            }
        },
        methods: {
            ...upload,
            handleClose() {
                this.$notify.info('取消修改')
                this.$emit('cancel')
            },
            cancel() {
                this.handleClose()
            },
            //确认提交
            confirmUpdate() {
                if (this.imgUrl === '') {
                    this.file = null
                }
                const formData = {
                    file: this.file,
                    courseStr: JSON.stringify(this.course)
                }
                console.log(formData)
                updateCourseInfo(formData).then(() => {
                    this.$message.success('修改成功')
                    this.$nextTick(() => {
                        this.file = null
                        this.imgUrl = ''
                        this.HFfileList = []
                    })
                    this.$forceUpdate()
                    this.$emit('cancel')
                    this.$emit('updateSuccess')
                })
            },
            //判断是否为纯数字
            isCellNumber(val) {
                if (!/^[0-9]*$/.test(val)) {
                    return false;
                } else {
                    return true;
                }
            },
        }
    }
</script>

<style scoped>

</style>